Skip to content

improvement(workflow): seed start block on server side#3890

Merged
icecrasher321 merged 5 commits intostagingfrom
improvement/default-artifacts
Apr 2, 2026
Merged

improvement(workflow): seed start block on server side#3890
icecrasher321 merged 5 commits intostagingfrom
improvement/default-artifacts

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

@icecrasher321 icecrasher321 commented Apr 1, 2026

Summary

  • Seed start block on server side like we do when we create new workspace or copilot creates new workflow.
  • Add creating state into workflow hydration state machine to prevent lag between navigation and workflow load
  • Fix new workspace creation state machine issue

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 2, 2026 1:23am

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 1, 2026

PR Summary

Medium Risk
Changes workflow/workspace creation to persist default workflow state in the same DB transaction and updates client hydration/navigation behavior; mistakes could lead to partially seeded workflows or stuck loading states.

Overview
Workflow creation now seeds default workflow blocks server-side: POST /api/workflows builds default artifacts, saves them to normalized tables inside a DB transaction, and returns startBlockId/subBlockValues to the client.

Workspace creation was adjusted to seed the initial workflow state within the existing transaction (removing the follow-up seeding step), and workflow persistence utilities now accept an optional external transaction.

On the client, workflow creation flows switch to an optimistic “creating” hydration phase (new registry actions markWorkflowCreating/markWorkflowCreated) to prevent premature loads/room joins during navigation; workspace list caching is also updated to merge newly created workspaces into the active cache to avoid redirect races during validation.

Written by Cursor Bugbot for commit 4f1bc29. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 1, 2026

Greptile Summary

This PR improves workflow creation by moving start block seeding to the server side (previously done client-side via a separate PUT /api/workflows/:id/state call) and introduces a new creating hydration phase to eliminate the lag between optimistic navigation and workflow data load.

Key changes:

  • Server-side seeding: POST /api/workflows now calls buildDefaultWorkflowArtifacts() and saveWorkflowToNormalizedTables() directly; if the normalized-table save fails it throws (returns 500) rather than silently returning 200 with possibly-missing state — addressing the previous concern around silent inconsistency
  • New creating hydration phase: Added to HydrationPhase and guarded in both the workflow-loading and redirect effects in workflow.tsx, preventing premature state loads or stale redirects while the mutation is in-flight
  • Optimistic navigation: Both handleCreateWorkflow and handleCreateWorkflowInFolder now fire-and-forget (mutate instead of mutateAsync) and navigate immediately; markWorkflowCreating is called synchronously before router.push so the guard is always in place when the new route renders
  • State machine transitions: onSuccess calls markWorkflowCreated(id)loadWorkflowState(id); onError calls markWorkflowCreated(null) → phase resets to idle, at which point workflow.tsx's existing redirect logic redirects the user to a valid workflow
  • Socket coordination: Socket room join is deferred while hydrationPhase === 'creating', preventing a join for a workflow whose server record may not yet exist
  • Stale-request guard extended: The loadWorkflowState error path now also discards stale errors (matching the already-present success-path guard)

Confidence Score: 5/5

  • Safe to merge — logic is well-structured, stale-request guards are correct, and error paths are handled; only remaining finding is a stale type annotation.
  • The one open comment is a P2 type-annotation mismatch (return type says Promise<string | null> but implementation never returns null). All previous concerns around silent inconsistency are resolved by the server-side throw on save failure. The hydration state machine transitions are sound, stale-request guards cover both success and error paths, and the socket coordination is correct.
  • apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workflow-operations.ts — stale return type annotation

Important Files Changed

Filename Overview
apps/sim/app/api/workflows/route.ts Server-side start block seeding added via buildDefaultWorkflowArtifacts + saveWorkflowToNormalizedTables; throws on save failure (preventing silent inconsistency); returns startBlockId/subBlockValues only on full success.
apps/sim/stores/workflows/registry/store.ts Adds markWorkflowCreating/markWorkflowCreated actions; creating phase guards prevent premature loads; stale request guard also correctly added to error path in loadWorkflowState; design is sound.
apps/sim/hooks/queries/workflows.ts Removes client-side state seeding; reads startBlockId/subBlockValues from server; guards subBlockValues update; calls markWorkflowCreated(id) on success and markWorkflowCreated(null) on error for proper state machine transition.
apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx Guards both workflow-loading and redirect effects against 'creating' phase; prevents premature navigation/load while mutation is in-flight.
apps/sim/app/workspace/[workspaceId]/w/components/sidebar/hooks/use-workflow-operations.ts Switches from mutateAsync to fire-and-forget mutate with optimistic navigation; return type annotation still says Promise<string
apps/sim/app/workspace/providers/socket-provider.tsx Subscribes to hydrationPhase and skips workflow room join while in 'creating' phase; dependency array correctly updated; unused useWorkflowDiffStore import removed.

Sequence Diagram

sequenceDiagram
    participant UI as UI (sidebar)
    participant Store as WorkflowRegistry
    participant Mutation as useCreateWorkflow
    participant Server as POST /api/workflows
    participant Socket as SocketProvider
    participant WF as workflow.tsx

    UI->>Mutation: mutate({ id, name, color })
    UI->>Store: markWorkflowCreating(id) → phase='creating'
    UI->>WF: router.push(/w/id)

    WF-->>WF: effect fires, phase='creating' → return early (no load)
    Socket-->>Socket: effect fires, phase='creating' → skip room join

    Mutation->>Server: POST /api/workflows
    Server->>Server: buildDefaultWorkflowArtifacts()
    Server->>Server: saveWorkflowToNormalizedTables() — throws on failure
    Server-->>Mutation: 200 { id, startBlockId, subBlockValues }

    alt Success
        Mutation->>Store: markWorkflowCreated(id)
        Store->>Store: loadWorkflowState(id) → phase='state-loading'
        Socket-->>Socket: phase≠'creating' → join-workflow room
        Store-->>WF: phase='ready'
    else Error
        Mutation->>Store: markWorkflowCreated(null) → phase='idle'
        WF-->>WF: redirect effect fires, currentWorkflowExists=false → redirect
    end
Loading

Reviews (2): Last reviewed commit: "add creating state machine for optimisti..." | Re-trigger Greptile

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

@greptile

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

@icecrasher321 icecrasher321 merged commit 27a11a2 into staging Apr 2, 2026
12 checks passed
@icecrasher321 icecrasher321 deleted the improvement/default-artifacts branch April 2, 2026 02:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant